package com.source.hotelbath.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.source.hotelbath.R;
import com.source.hotelbath.base.CustomerAdapter;
import com.source.hotelbath.model.GetOrderBean;
import com.source.hotelbath.model.GoodsListBean;
import com.source.hotelbath.model.PostBean;
import com.source.hotelbath.net.RequestConstants;
import com.source.hotelbath.utils.TintDrawable;
import com.source.hotelbath.utils.dialog.CustomerPromptDlg;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

/**
 * Created by ZhouMeng on 2017/10/8.
 */

public class OrderItemAdapter extends BaseAdapter {
    private Context mContext;
    private List<GetOrderBean> goodsList=new ArrayList<>();
    private int goodsNum;
    private String orderMoney;
    private int SUBMIT=0;
    private int ITEM=1;
   private Handler myHandler;
    private String status;
    public OrderItemAdapter(Context context, List<GetOrderBean> goodsList, Handler myHandler, String status) {
        mContext=context;
        this.goodsList=goodsList;
        this.myHandler=myHandler;
        this.status=status;
    }

    public void add(int goodsNum, String orderMoney){
        this.goodsNum=goodsNum;
        this.orderMoney=orderMoney;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        if (goodsList.size()>0){
            return goodsList.size()+1;
        }else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public int getItemViewType(int position) {
        if (goodsList.size()==position){
            return SUBMIT;
        }else {
            return ITEM;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position)==SUBMIT){
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_tv, null);
                holder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
                holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
                holder.tv_totle= (TextView) convertView.findViewById(R.id.tv_totle);
                holder.tv_num_get= (TextView) convertView.findViewById(R.id.tv_num_get);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


                holder.tv_goods_name.setText("合计");
                holder.tv_price.setText("-");
                holder.tv_num.setText(goodsNum+"");
                holder.tv_totle.setText(orderMoney);
                holder.tv_num_get.setText(goodsNum+"");

        }else {
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order, null);
                holder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
                holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
                holder.tv_totle= (TextView) convertView.findViewById(R.id.tv_totle);
                holder.tv_num_get= (TextView) convertView.findViewById(R.id.tv_num_get);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

                holder.tv_goods_name.setText(goodsList.get(position).getGoodsName());
                holder.tv_price.setText(goodsList.get(position).getUnitPrice());
                holder.tv_num.setText(goodsList.get(position).getNum()+"");
                if (goodsList.get(position).getNumGet()==0){
                    if ( goodsList.get(position).isChange()){
                        holder.tv_num_get.setText(0+"");
                        holder.tv_totle.setText(0);
                    }else {
                        holder.tv_num_get.setText(goodsList.get(position).getNum()+"");
                        holder.tv_totle.setText(goodsList.get(position).getTotalPrice());
                    }

                }else {
                    holder.tv_num_get.setText(goodsList.get(position).getNumGet()+"");
                    holder.tv_totle.setText(TintDrawable.mul(new BigDecimal(goodsList.get(position).getUnitPrice()),goodsList.get(position).getNumGet()+"")+"");
                }
            if ("04".equals(status)){
//                holder.tv_num_get.setBackgroundResource(R.drawable.gray_liner_bian);
            }else {
                holder.tv_num_get.setBackgroundResource(R.drawable.gray_liner_bian);
                holder.tv_num_get.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog(position);
                    }
                });
            }




        }


        return convertView;
    }


    class ViewHolder {

        TextView tv_goods_name;
        TextView tv_price;
        TextView tv_num;
        TextView tv_totle;
        TextView tv_num_get;
        EditText et_num_get;

    }
    private BigDecimal prices;

    private void showDialog(final int position) {

        CustomerPromptDlg dlg = new CustomerPromptDlg(mContext);
        dlg.setMsg(goodsList.get(position).getGoodsName()+"-实收");
        dlg.setListener(new CustomerPromptDlg.DlgListener() {
            @Override
            public void confirm(String s) {
                if (TextUtils.isEmpty(s.toString())){
//                    goodsList.get(position).setNumGet(0);

                }else {
                    goodsList.get(position).setChange(true);
                    goodsList.get(position).setNumGet(Integer.parseInt(s.toString()));

                }
                     goodsNum=0;
                     prices=new BigDecimal("0");
                     for (int i=0;i<goodsList.size();i++){
                         BigDecimal bd=new BigDecimal(goodsList.get(i).getUnitPrice());
                         if (goodsList.get(i).isChange()){
                             goodsNum+=goodsList.get(i).getNumGet();
                             BigDecimal price=TintDrawable.mul(bd,goodsList.get(i).getNumGet()+"");
                             prices= TintDrawable.add(prices,price);
                         }else {
                             goodsNum+=goodsList.get(i).getNum();
                             BigDecimal price=TintDrawable.mul(bd,goodsList.get(i).getNum()+"");
                             prices= TintDrawable.add(prices,price);
                         }


                     }
                Message message=Message.obtain();
                message.obj=prices+"";
                message.arg1=goodsNum;
                message.what=1;
                myHandler.sendMessage(message);
                    orderMoney=prices+"";


                notifyDataSetChanged();
            }

            @Override
            public void cancel() {

            }
        });
        dlg.show();
    }
}
